import pygame
from pygame.locals import *
import random

Tube_list = []
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((700, 500))
start_time=0
font = pygame.font.SysFont("华文仿宋", 35)

def star():
    global Tube_up1
    global Tube_dowm1
    global sprite_list
    choot=9
    score=0
    pygame.mixer.music.load("./musicgame/主界面BGM.mp3")
    wings_sound = pygame.mixer.Sound("./musicgame/翅膀.mp3")
    # crash_sound = pygame.mixer.Sound("music/me_down.wav")

    background1 = pygame.image.load('./图片/background1.png').convert()
    background2 = pygame.image.load('./图片/background2.png').convert()
    background3 = pygame.image.load('./图片/background3.png').convert()
    pygame.mixer.music.play(-1)
    bird_humber = Bird(screen)
    pygame.display.set_caption('像素小鸟')

    while True:
        append_Tube()
        font_view = font.render('分数 = %s' % str(score), True, (255, 255, 255))

        end_time = pygame.time.get_ticks()
        if end_time - start_time>=50:
            if choot%3==0:
                screen.blit(background1, (0, -40))
                choot+=1
            elif choot%2==0:
                screen.blit(background2, (0, 0))
                choot+=1
            elif choot%1==0:
                screen.blit(background3, (0, 0))
                choot=9

        bird_humber.display()

        sport()
        for event in pygame.event.get():
            # 判断是否是点击了退出按钮
            if event.type == QUIT:
                print("exit")
                exit()
            # 判断是否是按下了键
            elif event.type == KEYDOWN:
                # 检测按键是否是空格键
                if event.key == K_SPACE:
                    wings_sound.play()
                    bird_humber.move_up()
                elif event.key == K_ESCAPE:
                    exit()
        for i in range(len(Tube_list)):
            if bird_humber.x== Tube_list[i].x:
                score = score + 1

        for i in range(0,len(Tube_list),2):
            list=[]
            if bird_humber.x == Tube_list[i+1].x-10:
                for j in range(Tube_list[i+1].y-90,Tube_list[i+1].y-50):
                    list.append(j)
            if bird_humber.x == Tube_list[i+1].x:
                for j in range(Tube_list[i+1].y-90,Tube_list[i+1].y-50):
                    list.append(j)
            if bird_humber.x == Tube_list[i+1].x+10:
                for j in range(Tube_list[i+1].y-90,Tube_list[i+1].y-50):
                    list.append(j)

            if int(bird_humber.y) not in list and int(bird_humber.x) == Tube_list[i+1].x-10:
                exit()

            if int(bird_humber.y) not in list and int(bird_humber.x) == Tube_list[i+1].x:
                exit()

            if int(bird_humber.y) not in list and int(bird_humber.x) == Tube_list[i+1].x+10:
                exit()
        screen.blit(font_view, (10, 5))
        pygame.display.update()

class Bird():
    def __init__(self, screen):
        self.x = 10
        self.y = 200
        self.screen = screen
        self.image = pygame.image.load('./图片/bird1.png').convert_alpha()
        self.image2 = pygame.image.load('./图片/bird2.png').convert_alpha()


    def display(self):
        end_time = pygame.time.get_ticks()
        if end_time - start_time>=100:
                self.screen.blit(self.image2, (self.x, self.y))
        else:
            self.screen.blit(self.image, (self.x, self.y))
        self.y += 0.2
        if self.y>=490:
            exit()

    def move_up(self):
        if self.y<=20:
            self.y=self.y

        else:
            self.y -= 55

class Tube_up():
    def __init__(self, y, screen):

        self.x = 700
        self.y = y
        self.screen = screen
        self.image = pygame.image.load('./图片/shang.png').convert_alpha()


    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        self.x -= 0.5

    #     need_del_list = []
    #     for item in Tube_list:
    #         if item.judge():
    #             need_del_list.append(item)
    #     for def_list in need_del_list:
    #         Tube_list.remove(def_list)
    #
    # def judge(self):
    #     if self.x < -10:
    #         return True
    #     else:
    #         return False


class Tube_dowm():
    def __init__(self, y, screen):
        self.x = 700
        self.y = y
        self.screen = screen
        self.image = pygame.image.load('./图片/xia.png').convert_alpha()

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        self.x -= 0.5
        # need_del_list = []
        # for item in Tube_list:
        #     if item.judge():
        #         need_del_list.append(item)
        # for def_list in need_del_list:
        #     Tube_list.remove(def_list)

    # def judge(self):
    #     if self.x < -10:
    #         return True
    #     else:
    #         return False


Tube_up1 = Tube_up(20, screen)
Tube_dowm1 = Tube_dowm(0, screen)

def append_Tube():
    global start_time
    global Tube_up1
    global Tube_dowm1
    pp = random.randint(-200,0)
    po = pp + 400
    Tube_up1 = Tube_up(pp, screen)
    Tube_dowm1 = Tube_dowm(po, screen)
    end_time = pygame.time.get_ticks()
    if end_time - start_time >= 1500:
        Tube_list.append(Tube_up1)
        Tube_list.append(Tube_dowm1)
        start_time = end_time


def sport():
    for Tu in Tube_list:
        Tu.display()



if __name__ == '__main__':
    star()
